﻿#include "QwtCustomPlot.h"

#include <qwt_plot_grid.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_curve.h>
#include <QPen>
#include <qwt_legend.h>
#include <qwt_legend_label.h>
#include <QLayout>
#include <qwt_legend.h>
namespace GUI
{
    QwtCustomPlot::QwtCustomPlot(QWidget* parent)
        : QwtPlot(parent)
    {
        // 初始化
        init();
    }

    QwtCustomPlot::~QwtCustomPlot()
    {
        // 清除所有曲线
        clearCurves();
        // 删除网格对象
        if(_grid)
        {
            delete _grid;
            _grid = nullptr;
        }
        // 删除画布对象
        if(_canvas)
        {
            delete _canvas;
            _canvas = nullptr;
        }
    }

    void QwtCustomPlot::init()
    {
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        // 初始化网格
        initGrid();
        // 初始化画布
        initCanvas();
    }

    void QwtCustomPlot::initGrid()
    {
        // 配置网格，设置网格线的样式、颜色等
        if(!_grid)
        {
            //灰色 点线样式
            QPen pen(Qt::gray, 0, Qt::DotLine);
            _grid = new QwtPlotGrid;
            // 启用最小的X轴网格
            _grid->enableXMin(true);
            // 设置主网格线
            _grid->setMajorPen(pen);
            // 设置次网格线
            _grid->setMinorPen(pen);
            // 将网格附加到当前绘图对象
            _grid->attach(this);
        }
    }

    void QwtCustomPlot::initCanvas()
    {
        //设置绘图的画布样式，包括边框、背景色等属性。
        if(!_canvas)
        {
            _canvas = new QwtPlotCanvas;
            _canvas->setLineWidth(1);//设置画布边框宽度
             // 设置画布框架样式
            _canvas->setFrameStyle(QFrame::Box | QFrame::Plain);
            // 设置画布圆角半径
            _canvas->setBorderRadius(5);
            // 设置画布为白色
            QPalette canvasPalette(Qt::white);
            canvasPalette.setColor(QPalette::Foreground, QColor(133, 190, 232));
            // 应用调色板
            _canvas->setPalette(canvasPalette);
            // 将画布设置为当前的绘图画布
            setCanvas(_canvas);
            _canvas->setMouseTracking(true);
            // 启用自动重新绘制
            setAutoReplot(true);
        }
    }

    void QwtCustomPlot::setAxisName(QString xName, QString yName)
    {
        // 设置X轴和Y轴的标题
        setAxisTitle(xBottom, xName);
        setAxisTitle(yLeft, yName);
    }

    void QwtCustomPlot::setAxisXName(QString xName)
    {
        setAxisTitle(xBottom, xName);
    }

    void QwtCustomPlot::setAxisYName(QString yName)
    {
        setAxisTitle(yLeft, yName);
    }

    void QwtCustomPlot::setTitle(QString plotName)
    {
        // 设置绘图的主标题
        QwtPlot::setTitle(plotName);
    }
    void QwtCustomPlot::initLegend()
    {
        if (_legend!= nullptr)
        {
            delete _legend;
        }
        _legend = new QwtLegend;
        _legend->setDefaultItemMode(QwtLegendData::Checkable);
        insertLegend(_legend, QwtPlot::RightLegend);
        connect(_legend, &QwtLegend::checked, this, &QwtCustomPlot::slot_legendClicked);
    }

    void QwtCustomPlot::slot_legendClicked(const QVariant & itemInfo, bool checked)
    {
        QwtPlotItem *plotItem = infoToItem(itemInfo);
        if (plotItem)
        {
            plotItem->setVisible(checked);
        }
    }

    void QwtCustomPlot::setLegendChecked(int index, bool isCheckked)
    {
        const QWidget* contents = _legend->contentsWidget();
        if (contents)
        {
            QLayout* layoutLabel = contents->layout();
            if (layoutLabel)
            {
                QLayoutItem* item = layoutLabel->itemAt(index);
                if (item)
                {
                    QwtLegendLabel* legendLabel = (QwtLegendLabel*)(item->widget());
                    if (legendLabel)
                    {
                        legendLabel->setChecked(isCheckked);
                    }
                }
            }
        }
    }

    void QwtCustomPlot::updateAllLegendChecked()
    {
        QwtPlotItemList items = itemList(QwtPlotItem::Rtti_PlotCurve);
        for (int i = 0; i < items.size(); i++)
        {
            const QVariant itemInfo = itemToInfo(items[i]);

            QwtLegendLabel *legendLabel =
                qobject_cast<QwtLegendLabel *>(_legend->legendWidget(itemInfo));
            if (legendLabel)
                legendLabel->setChecked(true);//

            items[i]->setVisible(true);
        }

    }

    void QwtCustomPlot::addCurve(QVector<float> xData, QVector<float> yData, QColor color, QString name /*= "Curve"*/)
    {
        QwtPlotCurve *curve = new QwtPlotCurve;
        _curvePlotList.push_back(curve);
        curve->setTitle(name);
        // 设置曲线的样式
        QwtSymbol* symbol = new QwtSymbol;
        symbol->setSize(_dotSize, _dotSize);
        symbol->setPen(Qt::blue);
        symbol->setBrush(Qt::blue);
        symbol->setStyle(QwtSymbol::Rect);
        curve->setStyle(QwtPlotCurve::CurveStyle::Lines);
        curve->setSymbol(symbol);
        curve->setPen(QPen(color, _curvePenWidth));
        curve->setRenderHint(QwtPlotItem::RenderAntialiased, true);
        curve->setSamples(xData, yData);
        curve->setItemAttribute(QwtPlotItem::Legend, true);
        // 将曲线附加到当前绘图对象
        curve->attach(this);
        replot();
    }
    void QwtCustomPlot::addCurve(QVector<QPointF> data, QColor color,QString name)
    {
        QwtPlotCurve *curve = new QwtPlotCurve;
        curve->setTitle(name);

        QwtSymbol* symbol = new QwtSymbol;
        symbol->setSize(_dotSize, _dotSize);
        symbol->setPen(Qt::red);

        curve->setSymbol(symbol);
        curve->setPen(QPen(color, _curvePenWidth));
        curve->setRenderHint(QwtPlotItem::RenderAntialiased, true);
        curve->setLegendAttribute(QwtPlotCurve::LegendAttribute::LegendShowSymbol, true);
        curve->setLegendAttribute(QwtPlotCurve::LegendAttribute::LegendShowLine, true);
        curve->setSamples(data);
        curve->attach(this);
        _curvePlotList.push_back(curve);
        replot();
    }
    void QwtCustomPlot::setCurveData(QVector<float> xData, QVector<float> yData, QString name)
    {
        //设置曲线数据，绘制一条曲线
        if(!_curve)
        {
            _curve = new QwtPlotCurve;
            _curvePlotList.push_back(_curve);
        }

        _curve->setTitle(name);
        // 设置曲线的样式
        QwtSymbol* symbol = new QwtSymbol;
        symbol->setSize(5, 5);
        symbol->setPen(Qt::blue);
        symbol->setBrush(Qt::blue);
        symbol->setStyle(QwtSymbol::Rect);
        _curve->setStyle(QwtPlotCurve::CurveStyle::Lines);
        _curve->setSymbol(symbol);
        _curve->setPen(QPen(Qt::blue, 2));
        _curve->setRenderHint(QwtPlotItem::RenderAntialiased, true);
        _curve->setSamples(xData, yData);
        // 将曲线附加到当前绘图对象
        _curve->attach(this);
        // 绘制
        replot();
    }

    void QwtCustomPlot::clearCurves()
    {
        //修改关闭软件崩溃问题
        while (_curvePlotList.count()>0)
        {
            QwtPlotCurve *curve = _curvePlotList.at(0);
            if (curve)
            {
                removeItem(curve);
                delete curve;
                _curvePlotList.removeAt(0);
            }
        }
    }
}
